
a = c(0,0,0,0,0,
      0,1,1,1,0,
      0,1,0,1,0,
      0,1,0,1,0,
      0,0,0,0,0)

# setup
dx = 1
dy = 5

center =  13

# chain-code numbered from 0 to 8 pr Eliptic Fourier Features paper
direction = c(dx,dx-dy,-dy,-dy-dx,-dx,-dx+dy,+dy,dx+dy)
chain = c()
dir = 4


# find first blank going back from center
startpos = center
while(a[startpos+direction[dir+1]]){
  startpos = startpos+direction[dir+1]
}

curpos = startpos
# go around 
steps = 0
while(!steps | curpos!=startpos){
  if(a[curpos+direction[dir+1]]){
    # found next candidate
    chain = c(chain,dir)
    steps = steps + 1
    curpos = curpos+direction[dir+1]
    
    dir = (dir+3)%%8

  }
  else{
    dir = (dir-1)%%8
  } 
  
}

# notes, exits first time it visits start again.. might miss a few pixels because of this






